/**
 * 通用的工具函数
 */

const log4js = require('./logger');
const jwt = require('./jwt');

const menuDB = require('../models/menuSchema');
const userDB = require('../models/userSchema');
const roleDB = require('../models/roleSchema');
const deptDB = require('../models/deptSchema');
const userIdDB = require('../models/userIdSchema');
const testData = require('../db/testData');

const CODE = {
  SUCCESS: 0,
  PARAM_ERROR: 10001, // 参数错误
  USER_ACCOUNT_ERROR: 20001, // 账号或密码错误
  USER_LOGIN_ERROR: 30001, // 用户未登录
  BUSINESS_ERROR: 40001, // 业务请求失败
  AUTH_ERROR: 50001, // 认证失败或TOKEN过期
};

module.exports = {
  // 初始化测试数据库
  async resetDb() {
    log4js.info('准备开始初始化数据库...');
    // 初始化用户
    await userDB.deleteMany({});
    await userDB.insertMany(testData.user);
    // 初始化用户id索引
    await userIdDB.deleteMany({});
    await userIdDB.insertMany(testData.userId);
    // 初始化部门
    await deptDB.deleteMany({});
    await deptDB.insertMany(testData.dept);
    // 初始化菜单
    await menuDB.deleteMany({});
    await menuDB.insertMany(testData.menu);
    // 初始化角色
    await roleDB.deleteMany({});
    await roleDB.insertMany(testData.role);
    log4js.info('初始化数据库完成！');
  },
  /**
   * 分页结构封装
   * @param {number} pageNum
   * @param {number} pageSize
   * @returns {{skipIndex: number, page: {pageSize: number, pageNum: number}}}
   */
  pager({ pageNum = 1, pageSize = 10 }) {
    pageNum *= 1;
    pageSize *= 1;
    const skipIndex = (pageNum - 1) * pageSize;
    return { page: { pageNum, pageSize }, skipIndex };
  },
  success(data = '', msg = '请求成功', code = CODE.SUCCESS) {
    log4js.debug(data);
    return { code, data, msg };
  },
  fail(msg = '业务请求失败,请重试。', code = CODE.BUSINESS_ERROR, data = '') {
    log4js.debug(msg);
    return { code, data, msg };
  },
  failAuth(msg = 'TOKEN认证失败或TOKEN过期，请重新登录。', code = CODE.AUTH_ERROR, data = '') {
    log4js.debug(msg);
    return { code, data, msg };
  },
  failParam(msg = '参数错误，请重试。', code = CODE.PARAM_ERROR, data = '') {
    log4js.debug(msg);
    return { code, data, msg };
  },
  // 递归拼接树形列表
  getTreeMenu(rootList, id, list) {
    for (let i = 0; i < rootList.length; i++) {
      let item = rootList[i];
      if (String(item.parentId.slice().pop()) == String(id)) {
        list.push(item._doc);
      }
    }
    list.map((item) => {
      item.children = [];
      this.getTreeMenu(rootList, item._id, item.children);
      if (item.children.length === 0) {
        delete item.children;
      } else if (item.children[0].menuType == 2) {
        // 快速区分按钮和菜单
        item.action = item.children;
      }
    });
    return list;
  },
  // 根据token 获取用户 信息
  getTokenInfo(authorization) {
    let token = authorization.split(' ')[1];
    let { data } = jwt.verify(token);
    return data;
  },
  // 格式化日期
  formateDate(date, rule) {
    let fmt = rule || 'yyyy-MM-dd hh:mm:ss';
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, date.getFullYear());
    }
    const o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds(),
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(fmt)) {
        const val = o[k] + '';
        fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? val : ('00' + val).substr(val.length));
      }
    }
    return fmt;
  },
};
